package com.smarthome.smart;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

public class Notes {
    
    public static final String DATABASE_NAME = "data";
    public static final String DATABASE_TABLE = "notes";
    public static final int DATABASE_VERSION = 1;
    public static final String TABLE_CREATE ="create table notes (_id integer primary key autoincrement, "
                							  + "title text not null, body text not null);";
    
    public static final String COL_TITLE = "title";
    public static final String COL_BODY = "body";
    
    private int id;
    private String title;
    private String body;
    
    public int getId() {
        return id;
    }
    public String getTitle() {
        return title;
    }
    public String getBody() {
        return body;
    }
    public void setId(int id) {
        this.id = id;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public void setBody(String body) {
        this.body = body;
    }
    
    
  public String testCRUD(){
        
        String result="";
        Cursor cursor = null;
        String[] columns = new String[] {SmartCRUD.KEY_ID, Notes.COL_TITLE, Notes.COL_BODY};
        
        
        SmartCRUD dao = SmartCRUD.getInstance(null, Notes.DATABASE_NAME, Notes.TABLE_CREATE, Notes.DATABASE_TABLE, Notes.DATABASE_VERSION);

        if(dao != null){
            
            ContentValues values = new ContentValues();
            values.put(Notes.COL_TITLE, "aaa");
            values.put(Notes.COL_BODY, "bbb");
            dao.insert(Notes.DATABASE_TABLE, values);
            
            values = new ContentValues();
            values.put(Notes.COL_TITLE, "ccc");
            values.put(Notes.COL_BODY, "ddd");
            dao.insert(Notes.DATABASE_TABLE, values);
            
            cursor = dao.get(Notes.DATABASE_TABLE, columns);
            
            
            int idColumn = cursor.getColumnIndex(SmartCRUD.KEY_ID); 
            int titleColumn = cursor.getColumnIndex(Notes.COL_TITLE); 
            int bodyColumn = cursor.getColumnIndex(Notes.COL_BODY); 
            
            if(cursor != null){
                if(cursor.moveToFirst()){
                    
                    int count = cursor.getCount();
                    result = "tem " + count + " registros.";
                    List<Notes> list = new ArrayList<Notes>();
                    
                    for(int i=0; i<count; i++){
                        
                        int id = cursor.getInt(idColumn); 
                        String title = cursor.getString(titleColumn); 
                        String body = cursor.getString(bodyColumn); 
                        
                        Notes notes = new Notes();
                        notes.setId(id);
                        notes.setTitle(title);
                        notes.setBody(body);
                        
                        list.add(notes);
                        
                        result += " " + i + ": " + "id=" + id + ", title=" + title + ", body=" + body + ";";
                        
                        cursor.moveToNext();
                    }
                }
            }
           
            result += "agora atualizar o segundo registro.";
            
            values = new ContentValues();
            values.put(Notes.COL_TITLE, "eee");
            values.put(Notes.COL_BODY, "fff");
            dao.update(Notes.DATABASE_TABLE, 2, values);
            
            cursor.requery();
            cursor.close();

            result += " agora apagar primeiro registro.";
            
            dao.delete(Notes.DATABASE_TABLE, 1);
            
            result += " now delete all records.";
            
            dao.delete(Notes.DATABASE_TABLE);
            
            dao.close();
        }
        
        return result;
    }
    
}

